package day24;




 class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
      this.val = val;
    }
 }


public class A1 {


    /**
     * 判断是否为平衡二叉树
     * @param pRoot
     * @return 返回-1表明不是平衡树，返回其他则是树的高度
     */
    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if(pRoot==null) return true;
        int ret = deep(pRoot);
        return ret>=0;
    }
    private int deep(TreeNode root) {
        if(root==null) return 0;
        int left = deep(root.left);
        if(left==-1) return -1;
        int right = deep(root.right);
        if(right==-1) return -1;
        int gap = Math.abs(left-right);
        return gap<2?Math.max(left,right)+1:-1;
    }
}
